package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j
public class DishServiceimpl implements DishService {
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private DishFlavorMapper dishFlavorMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    /**
     * 保存菜品及其配料信息
     * @param dishDTO
     */
    @Transactional
    public void saveWithFlavors(DishDTO dishDTO) {
        Dish dish = new Dish();
        //将dto对象中的属性复制到dish对象中
        BeanUtils.copyProperties(dishDTO, dish);
        //向dish表中插入1条菜品信息
        dishMapper.insert(dish);
        //获取刚插入的菜品的id
        Long dishId = dish.getId();

        //向dish_flavor表中插入N个配料信息
        List<DishFlavor> flavors = dishDTO.getFlavors();
        //判断是否有配料信息
        if (flavors != null && flavors.size() > 0) {
            //给配料信息设置dish_id
            flavors.forEach(dishflavor -> {
                dishflavor.setDishId(dishId);
            });
            //向dish_flavor表中插入N条配料信息
            dishFlavorMapper.insertBatch(flavors);
        }

    }

    /**
     * 分页查询菜品信息
     * @param dishPageQueryDTO
     * @return
     */
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> page = dishMapper.pageQuery(dishPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 根据ID批量删除菜品信息
     * @param ids
     */
    @Transactional//由于涉及到多个表的操作，需要使用事务注解，保证删除操作的原子性
    public void deleteBatch(List<Long> ids) {
        //判断当前菜品是否能够被删除-是否存在起售中的菜品
        for (Long id : ids) {
            Dish dish = dishMapper.getByID(id);
            if (dish.getStatus() == StatusConstant.ENABLE) {
                //抛出异常，提示菜品已上架，不能删除
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }

        }

        //是否被关联
        List<Long> setmealIds = setmealDishMapper.getSetmealIdsByDishId(ids);
        if (setmealIds != null && setmealIds.size() > 0) {
            //抛出异常，提示菜品已关联套餐，不能删除
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        //删除菜品信息
        for (Long id : ids) {
            dishMapper.deleteByID(id);
            //删除菜品口味信息
            dishFlavorMapper.deleteByDishId(id);
        }
    }
}
